<template>
  <CommonPage title="系统参数定义">
    <template #mid>
      <SearchForm>
        <template #extraButtons>
          <a-button type="primary" @click="showCreate">新增系统参数</a-button>
        </template>
      </SearchForm>
    </template>
    <SlTable :columns="columns" row-key="id" :data-source="tableData" :pagination="pagination" :loading="tableLoading">
      <template #bodyCell="{ column, record }: BodyCellOptions<Partial<ParamItem>>">
        <div v-if="column.key === 'oper'">
          <a @click="showUpdateModal(record)">编辑</a>
          <a class="ml-2 text-red-400" @click="handleDelete(record)">删除</a>
        </div>
      </template>
    </SlTable>
    <CURDDialog
      ref="CURDModalIns"
      id-key="id"
      :columns="columns"
      create-title="新增参数"
      update-title="编辑参数"
      delete-message="确认删除该参数吗？"
      :show-options="showOptions"
      :model="model"
      :model-form-setting="modelFormSetting"
      :table-data="tableData"
      :curd="curd"
      :reload-table="reloadTable"
    />
  </CommonPage>
</template>
<script lang="ts" setup>
import { ref, shallowRef } from "vue";
import { useI18n } from "/@/locales/useI18n";
import { useCURD } from "/@/hooks/useCURD";
import { FormItemComponentType, CURDAction, CURDDialogExpose } from "/@/components/CURDDialog/models";
import type { FormItemData, OperItem, ShowCURDModalOptions, CURDModalEvent } from "/@/components/CURDDialog/models";
import type { TableProps } from "ant-design-vue";
import { PatternKey } from "/@/utils/verify";
import { Api } from "/@/api/main/system";
import { MenuItem, MenuType } from "/@/api/home/menu/models";
import { handleMenuSort } from "/@/stores/menu";
import { loopItem } from "/@/utils/treeData";
import type { ParamItem } from "/@/api/main/system/models";
import dayjs from "dayjs";
import type { BodyCellOptions } from "/@/types/antd";

const { t } = useI18n();
const columns = ref<TableProps<Partial<MenuItem>>["columns"]>([
  {
    title: "名称",
    dataIndex: "name",
  },
  {
    title: "模块",
    dataIndex: "model",
  },
  {
    title: "参数",
    dataIndex: "param",
  },
  {
    title: "说明",
    dataIndex: "description",
  },
  {
    title: "创建时间",
    dataIndex: "createTime",
    customRender({ text }) {
      return dayjs(text).format("YYYY-MM-DD HH:mm:ss");
    },
  },
  {
    title: t("common.oper"),
    key: "oper",
    width: 100,
  },
]);
const model = ref<Partial<ParamItem>>({
  name: "",
  model: "",
  param: "",
  description: "",
});
const modelFormSetting: FormItemData<Partial<ParamItem>>[] = [
  {
    key: "name",
    max: 32,
    pattern: PatternKey.code,
    required: true,
  },
  {
    key: "model",
    max: 32,
    pattern: PatternKey.code,
    required: true,
  },
  {
    key: "param",
    required: true,
    type: FormItemComponentType.NUMBER,
  },
  {
    key: "description",
    type: FormItemComponentType.TEXTAREA,
  },
];

const { pagination, tableData, tableLoading, curd, reloadTable } = useCURD<ParamItem>({
  getPageUrl: Api.paramPage,
  cudUrl: Api.param,
});

const showOptions = ref<ShowCURDModalOptions>();
function showCreate() {
  showOptions.value = { action: CURDAction.CREATE };
}

function showUpdateModal(record) {
  showOptions.value = { action: CURDAction.UPDATE, record };
}

function handleDelete(record: Partial<ParamItem>) {
  showOptions.value = { action: CURDAction.DELETE, ids: [record.id as number] };
}
</script>
